import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Scanner;


public class StorageManager {
	SystemCatologReport syscat;
	public StorageManager(String input) throws IOException{
		syscat = new SystemCatologReport("syscatreport.txt");
	}
	public void deleteTable() throws IOException{
		String tableName = "";
		boolean ok = false;
		int page = -1;
		Scanner scan = new Scanner(System.in);
		while(!ok){
			int i=0;
			System.out.println("Please enter the name of the table: ");
			tableName = scan.nextLine();
			if(!isASCII(tableName)){
				System.out.println("Table name cannot contain any non-ASCII character!");
				i++;
			}
			if(tableName.length()>27){
				System.out.println("Length of the table name must be less than or equal to 27!");
				i++;
			}
			page = syscat.isInFile(tableName);
			System.out.println(page);
			if(page<0){
				System.out.println("There is not a table named "+tableName+"!");
				i++;
			}
			if(i==0){
				ok = true;
			}
		}
		syscat.deleteATable(page/2, tableName);
	}
	public void addTable() throws IOException{
		String tableName = "";
		int numberOfFields = 0;
		boolean ok = false;
		ArrayList<String> fieldNames = new ArrayList<String>();
		ArrayList<Integer> fieldSizes = new ArrayList<Integer>();
		Scanner scan = new Scanner(System.in);
		while(!ok){
			int i=0;
			System.out.println("Please enter the name of the table: ");
			tableName = scan.nextLine();
			if(!isASCII(tableName)){
				System.out.println("Table name cannot contain any non-ASCII character!");
				i++;
			}
			if(tableName.length()>27){
				System.out.println("Length of the table name must be less than or equal to 27!");
				i++;
			}
			if(syscat.isInFile(tableName)>=0){
				System.out.println("There is already a table named "+tableName+"!");
				i++;
			}
			if(i==0){
				ok=true;
			}
		}
		ok=false;
		while(!ok){
			int i=0;
			System.out.println("Please enter the number of fields: ");
			numberOfFields = scan.nextInt();
			if(numberOfFields>13 || numberOfFields<2){
				System.out.println("Number of fields must be between 2 and 13!");
				i++;
			}
			if(i==0){
				ok=true;
			}
		}
		System.out.println("Enter the names and sizes of the fields(First name you enter is going to be the name of the key field): ");
		for(int i=0; i<numberOfFields; i++){
			ok=false;
			while(!ok){
				int j=0;
				System.out.println("Enter the "+i+". field's name: ");
				String name = scan.next();
				if(name.length()>16){
					System.out.println("Length of a field name cannot be greater than 16!");
					j++;
				}
				else if(!isASCII(name)){
					System.out.println("Field name cannot contain any non-ASCII character!");
					j++;
				}
				if(j==0){
					ok =true;
					fieldNames.add(name);
				}	
			}
			ok = false;
			while(!ok){
				int j=0;
				System.out.println("Enter the "+i+". field's size: ");
				int size = scan.nextInt();
				if(size>15 || size<1){
					System.out.println("A size of a field must be between 1 and 15");
					j++;
				}
				if(j==0){
					ok = true;
					fieldSizes.add(size);
				}
			}
		}		
		TableRecord record = new TableRecord(tableName,numberOfFields,fieldNames,fieldSizes);
		syscat.addTable(record);
	}
	public boolean isASCII(String s){
		//char[] letters = new char[s.length()];
		char[] letters = s.toCharArray();
		for(char c : letters){
			if( c>127)
				return false;
		}
		return true;
	}
	public void listAllTables() throws IOException{
		ArrayList<String> list = syscat.listOfAllTables();
		for(String s : list){
			System.out.println(s);
		}
	}
	public void addDataRecord() throws IOException{
		String tableName = "";
		boolean ok = false;
		int page = -1;
		Scanner scan = new Scanner(System.in);
		while(!ok){
			int i=0;
			System.out.println("Please enter the name of the table: ");
			tableName = scan.nextLine();
			if(!isASCII(tableName)){
				System.out.println("Table name cannot contain any non-ASCII character!");
				i++;
			}
			if(tableName.length()>27){
				System.out.println("Length of the table name must be less than or equal to 27!");
				i++;
			}
			page = syscat.isInFile(tableName);
			System.out.println(page);
			if(page<0){
				System.out.println("There is not a table named "+tableName+"!");
				i++;
			}
			if(i==0){
				ok = true;
			}
		}
		TableRecord tableRecord = syscat.findTheRecord(tableName);
		System.out.println("Enter the values the fields(First value you enter is going to be the value of the key field): ");
		ArrayList<String> values = new ArrayList<String>();
		int numOfFields = tableRecord.getNumberOfFields();
		for(int i=0;i<numOfFields;i++){
			String value = "";
			ok = false;
			while(!ok){
				int j=0;
				System.out.println("Enter the value of the "+i+". field: ");
				value = scan.nextLine();
				if(!isASCII(value)){
					System.out.println("A value cannot contain any non-ASCII character!");
					j++;
				}
				if(value.length()>tableRecord.getFieldSizes().get(i)){
					System.out.println("A value cannot have a greater length than it has decided while creating the table!");
					j++;
				}
				if(j==0){
					ok=true;
				}
			}
			values.add(value);
		}
		DataFile dataFile = new DataFile(tableRecord);
		dataFile.addDataRecord(values);
	}
}
